package com.cl.mybatis.learn.day09;

import org.apache.ibatis.session.TransactionIsolationLevel;
import org.apache.ibatis.transaction.Transaction;
import org.apache.ibatis.transaction.jdbc.JdbcTransaction;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * 定义自己的事务管理器,实现获取连接、提交、回滚、关闭数据库连接等操作
 */
public class MyTransaction extends JdbcTransaction implements Transaction {
    
    public MyTransaction(Connection connection) {
        super(connection);
    }
    
    public MyTransaction(DataSource ds, TransactionIsolationLevel desiredLevel, boolean desiredAutoCommit) {
        super(ds, desiredLevel, desiredAutoCommit);
    }
    
    @Override
    public Connection getConnection() throws SQLException {
        System.out.println("获取连接");
        return super.getConnection();
    }
    
    @Override
    public void commit() throws SQLException {
        System.out.println("提交事务");
        super.commit();
    }
    
    @Override
    public void rollback() throws SQLException {
        System.out.println("回滚事务");
        super.rollback();
    }
    
    @Override
    public void close() throws SQLException {
        System.out.println("关闭连接");
        super.close();
    }
    
    @Override
    public Integer getTimeout() throws SQLException {
        System.out.println("获取超时时间");
        return super.getTimeout();
    }
}